package us.mooresr.aircraftscheduling.server.handler;

import java.util.ArrayList;

import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;

import us.mooresr.aircraftscheduling.server.DAO;
import us.mooresr.aircraftscheduling.shared.action.GetUserList;
import us.mooresr.aircraftscheduling.shared.action.GetUserListResult;
import us.mooresr.aircraftscheduling.shared.model.User;

import com.google.inject.Inject;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.shared.ActionException;

public class GetUserListActionHandler implements
		ActionHandler<GetUserList, GetUserListResult> {

	@Inject
	public GetUserListActionHandler() {
	}

	@Override
	public GetUserListResult execute(GetUserList action, ExecutionContext context)
			throws ActionException {
		DAO dao = new DAO();
		Boolean includeInactive = action.getIncludeInactive();
		Query<User> q;
		
		ArrayList<User> result = new ArrayList<User>();
		

		q = dao.ofy().query(User.class);

		
		for (User u : q) {
			if (u.getIsInactive()) {
				if (includeInactive) {
					result.add(u);
				}
			} else {
				result.add(u);
			}
		}
		
		return new GetUserListResult(result);
		
	}

	@Override
	public void undo(GetUserList action, GetUserListResult result,
			ExecutionContext context) throws ActionException {
	}

	@Override
	public Class<GetUserList> getActionType() {
		return GetUserList.class;
	}
}
